package com.zx.mes.aop.hyl.aopconfig;

import com.zx.mes.aop.hyl.service.AuthService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Modifier;

/**
 * auth aop config
 *
 * @author huayunlong
 * @date 2018-9-10
 */
@Aspect
@Component
@Slf4j
public class AuthAopConfig {

    @Autowired
    private AuthService authService;

    @Pointcut("execution(public * com.zx.mes.aop.hyl.service.*Impl.*(..))")
    public void check() {

    }


//    @Before("check()")
//    public void before() {
//        log.info("------------------------------ 方法执行之前 ------------------------------");
//        String id = "";
//        boolean b = authService.check(id);
//        log.info("1111111  auth check:" + b);
////        if (!b) {
////            new RuntimeException("您无权限");
////        }
//    }
//
//
//    @After("check()")
//    public void after() {
//        log.info("------------------------------ 方法执行之后 ------------------------------");
//    }

    @Around("check()")
    public void aroud(ProceedingJoinPoint pjp) throws Throwable {
        log.info("------------------------------ 方法执行之前 ------------------------------");
        long start = System.currentTimeMillis();
        /// 签名操作
        Signature signature = pjp.getSignature();
        log.info("22222222  ["+signature.getDeclaringType()+"]:["+signature.getDeclaringTypeName()+"]:"
                + Modifier.toString(signature.getModifiers())+":"
                +signature.getName());

        /// 调用方法的参数
        Object[] args = pjp.getArgs();
        log.info(args[0].toString());
        String id = args[0].toString();

        if (authService.check(id)) {
            Object proceed = pjp.proceed();
            log.info(proceed.toString());
        }else {
            log.info("您没有权限!");
        }

        log.info("------------------------------ 方法执行之前 ------------------------------");
        log.info("方法执行时间:"+(System.currentTimeMillis()-start)+"ms");
    }
}
